"""
    关键字驱动类：
        1.创建浏览器对象
        2.访问url
        3.定位元素
        4.输入
        5.点击
        6.关闭浏览器
        7.强制等待
"""
from time import sleep

from selenium import webdriver


# 不同浏览器生成
def open_browser(txt):
    try:
        #反射机制
        driver = getattr(webdriver, txt)()
    except Exception as e:
        print(e)
        driver = webdriver.Chrome()
    return driver


class Key:
    # 临时driver对象
    # driver = webdriver.Chrome()

    # 构造函数
    def __init__(self, txt):
        self.driver = open_browser(txt)
        self.driver.inplicitly_wait(10)

    # 访问url
    def open(self, txt):
        self.driver.get(txt)

    # 元素定位:一定要满足8种方法
    def locate(self, name, value):
        return self.driver.find_element(name, value)

    # 输入
    def input(self, name, value, txt):
        self.locate(name, value).send_keys(txt)

    # 点击
    def click(self, name, value):
        self.locate(name, value).click()

    # 强制等待
    def sleep(self, time):
        sleep(time)

    # 退出
    def quit(self):
        self.driver.quit()
